home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / MacPerl 5.1.3 / Mac_Perl_513_src / perl5.002 / lib / UNIVERSAL.pm < prev    next >
Encoding:
Perl POD Document  |  1996-06-03  |  2.1 KB  |  113 lines  |  [TEXT/ttxt]

  1. #
  2.  
  3. package UNIVERSAL;
  4.  
  5. =head1 NAME
  6.  
  7.     UNIVERSAL - Default general behaviour for all objects.
  8.  
  9. =head1 SYNOPSIS
  10.  
  11.     use UNIVERSAL;
  12.  
  13.     if($obj->isa('IO::Handle')) {
  14.         ...
  15.     }
  16.  
  17.     $func = $obj->can('some_method_name');
  18.  
  19.     $class = $obj->class;
  20.  
  21.     if($var->is_instance) {
  22.         ...
  23.     }
  24.  
  25. =head1 DESCRIPTION
  26.  
  27. The C<UNIVERSAL> package defines methods that are inherited by all other
  28. classes. These methods are
  29.  
  30. =over 4
  31.  
  32. =item isa( CLASS )
  33.  
  34. C<isa> returns I<true> if its object is blessed into a sub-class of C<CLASS>
  35.  
  36. C<isa> is also exportable and can be called as a sub with two arguments. This
  37. allows the ability to check what a reference points to. Example
  38.  
  39.     use UNIVERSAL qw(isa);
  40.  
  41.     if(isa($ref, 'ARRAY')) {
  42.         ...
  43.     }
  44.  
  45. =item can( METHOD )
  46.  
  47. C<can> checks to see if its object has a method called C<METHOD>,
  48. if it does then a reference to the sub is returned, if it does not then
  49. I<undef> is returned.
  50.  
  51. =item class()
  52.  
  53. C<class> returns the class name of its object.
  54.  
  55. =item is_instance()
  56.  
  57. C<is_instance> returns true if its object is an instance of some
  58. class, false if its object is the class (package) itself. Example
  59.  
  60.     A->is_instance();       # Flase
  61.     
  62.     $var = 'A';
  63.     $var->is_instance();    # False
  64.     
  65.     $ref = bless [], 'A';
  66.     $ref->is_instance();    # True
  67.  
  68. =back
  69.  
  70. =head1 NOTE
  71.  
  72. C<isa> and C<can> are implemented in XS code. C<can> directly uses perl's
  73. internal code for method lookup, and C<isa> uses a very similar method and
  74. cache-ing strategy. This may cause strange effects if the perl code
  75. dynamically changes @ISA in any package.
  76.  
  77. =head1 AUTHOR
  78.  
  79. Graham Barr <Graham.Barr@tiuk.ti.com>
  80.  
  81. =head1 COPYRIGHT
  82.  
  83. Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
  84. software; you can redistribute it and/or modify it under the same terms
  85. as Perl itself.
  86.  
  87. =head1 REVISION
  88.  
  89. $Revision: 1.3 $
  90.  
  91. =cut
  92.  
  93. $VERSION  = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
  94.  
  95. require DynaLoader;
  96. require Exporter;
  97.  
  98. @ISA = qw(Exporter DynaLoader);
  99. @EXPORT_OK = qw(isa);
  100.  
  101. bootstrap UNIVERSAL $VERSION;
  102.  
  103. sub is_instance {
  104.     ref($_[0]) ? 1 : ''
  105. }
  106.  
  107. sub class {
  108.     ref($_[0]) || $_[0]
  109. }
  110.  
  111. 1;
  112.  
  113.